package utils

import (
	"fmt"
	"math/rand"
	"time"
)

func GenUid(gameId, miniOpenId string) string {
	return StringMd5(fmt.Sprintf("%s:%s:%d", gameId, miniOpenId, time.Now().Nanosecond()))
}

func GenShareCode(uid string) string {
	return StringMd5(fmt.Sprintf("box:%s", uid))
}

func GenShareToken() string {
	randString := GetRandomString(8)
	return StringMd5(fmt.Sprintf("leyoubox:%s:%d", randString, time.Now().Nanosecond()))
}

func TodayUnix() string {
	timeStr := time.Now().In(time.Local).Format("2006-01-02")
	return timeStr
}

func YesterdayUnix() string {
	t := time.Now().In(time.Local).AddDate(0, 0, -1)
	timeStr := t.Format("2006-01-02")
	return timeStr
}

func GetRandomString(l int) string {
	str := "abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < l; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

func StringInSlice(sliceList []string, val string) bool {
	for _, value := range sliceList {
		if value == val {
			return true
		}
	}
	return false
}

//判断slice 中是否有int64值
func Int64InSlice(sliceList []int64, val int64) bool {
	for _, value := range sliceList {
		if value == val {
			return true
		}
	}
	return false
}

func GenOrderSn(uid, timestr string) string {
	return StringMd5(fmt.Sprintf("box:%s:%s", uid, timestr))
}
